package org.zzk.dao;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;

@Data
public class Room {
    private static final Logger logger = LoggerFactory.getLogger(Room.class);
    private final String id;
    private final List<Player> players;
    private final ChessGame game;

//    public synchronized GameResult makeMove(ChessMove move) {
//        return game.applyMove(move);
//    }

    public Optional<Player> getCurrentPlayer() {
        return Optional.of(players.get(game.getCurrentPlayerIndex()));
    }

    public Optional<Player> getOpponentPlayer() {
        return Optional.of(players.get(game.getCurrentPlayerIndex() == 0 ? 1 : 0));
    }

    public void initCurrentPlayer() {
        for (int i = 0; i < players.size(); i++) {
            if (players.get(i).isRed()) {
                logger.info("player {} is red", players.get(i).getId());
                game.setCurrentPlayerIndex(i);
            }
        }
    }

    public void setCurrentPlayer() {
        if (game.getCurrentPlayerIndex() == 0) {
            game.setCurrentPlayerIndex(1);
        } else {
            game.setCurrentPlayerIndex(0);
        }
    }
}
